//
//  LogisticsMessageViewController.m
//  ShrimpCrowdordering
//
//  Created by 郑洲 on 2018/7/27.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "LogisticsMessageViewController.h"
#import "LogisticsTableViewCell.h"
#import "LogisticsMessageModel.h"

@interface LogisticsMessageViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) BaseTableView  *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSDictionary   *companyCode;
@property (nonatomic, strong) UILabel        *companyLabel;
@property (nonatomic, strong) NSString       *code;

@end

@implementation LogisticsMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"物流信息";
    self.view.backgroundColor = RGB_COLOR(244, 244, 244);
    
    _companyCode = @{@"SF":@"顺丰速运",@"HTKY":@"百世快递",@"ZTO":@"中通快递",@"STO":@"申通快递",@"YTO":@"圆通速递",@"YD":@"韵达速递",@"YZPY":@"邮政快递包裹",@"EMS":@"EMS",@"HHTT":@"天天快递",@"JD":@"京东快递",@"UC":@"优速快递",@"DBL":@"德邦快递",@"ZJS":@"宅急送",@"TNT":@"TNT快递",@"UPS":@"UPS",@"DHL":@"DHL",@"FEDEX":@"FEDEX联邦(国内件）",@"FEDEX_GJ":@"FEDEX联邦(国际件)"};
    _dataArray = [NSMutableArray array];
    
    self.tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = ScreenX375(207);
    [self.view addSubview:self.tableView];
    [self getData];
}

- (void)copyText {
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string = self.orderNum;
    [HttpManager showNoteMsg:@"复制成功"];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }
    return 1 + _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        static NSString *cellId = @"logisticsNum";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        for (UIView *subView in cell.subviews) {
            [subView removeFromSuperview];
        }
        UILabel *nameNote = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(74), ScreenX375(28), ScreenX375(55), ScreenX375(16))];
        nameNote.text = @"物流公司";
        nameNote.textColor = REXADECIMALCOLOR(@"#696969");
        nameNote.font = Kfont(12);
        [cell addSubview:nameNote];
        
        UILabel *numberNote = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(156), ScreenX375(28), ScreenX375(55), ScreenX375(16))];
        numberNote.text = @"物流单号";
        numberNote.textColor = REXADECIMALCOLOR(@"#696969");
        numberNote.font = Kfont(12);
        [cell addSubview:numberNote];
        
        _companyLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(74), ScreenX375(45), ScreenX375(70), ScreenX375(18))];
        if (_code.length != 0) {
            _companyLabel.text = [self.companyCode valueForKey:self.code];
        }
        _companyLabel.textColor = REXADECIMALCOLOR(@"#000000");
        _companyLabel.font = Kfont(15);
        [cell addSubview:_companyLabel];
        
        UILabel *logisticsNum = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(156), ScreenX375(45), ScreenX375(180), ScreenX375(18))];
        logisticsNum.text = self.orderNum;
        logisticsNum.textColor = REXADECIMALCOLOR(@"#686868");
        logisticsNum.font = Kfont(15);
        [cell addSubview:logisticsNum];
        
        UIButton *copyBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(335), ScreenX375(40), ScreenX375(30), ScreenX375(28))];
        [copyBtn setTitle:@"复制" forState:UIControlStateNormal];
        [copyBtn setTitleColor:REXADECIMALCOLOR(@"#000000") forState:UIControlStateNormal];
        copyBtn.titleLabel.font = Kfont(12);
        [copyBtn addTarget:self action:@selector(copyText) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:copyBtn];
        
        return cell;
    }else if (indexPath.section == 1 && indexPath.row == 0) {
        static NSString *cellId = @"address";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        for (UIView *subView in cell.subviews) {
            [subView removeFromSuperview];
        }
        UIImageView *noteImage = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenX375(74), ScreenX375(45), ScreenX375(13), ScreenX375(15))];
        noteImage.image = ImageWithName(@"个人中心-地址管理");
        [cell addSubview:noteImage];
        
        UILabel *addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(100), 0, SCREEN_WIDTH - ScreenX375(124), ScreenX375(105))];
        addressLabel.text = self.acceptAdress;
        addressLabel.textColor = REXADECIMALCOLOR(@"#222222");
        addressLabel.font = Kfont(13);
        addressLabel.numberOfLines = 0;
        [cell addSubview:addressLabel];
        
        return cell;
    }else {
        LogisticsTableViewCell *cell = [LogisticsTableViewCell cellWithTableView:tableView andIndexPath:indexPath];
        [cell initWithModel:[_dataArray objectAtIndex:_dataArray.count - indexPath.row ]];
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return ScreenX375(90);
    }else if (indexPath.section == 1 && indexPath.row == 0) {
        return ScreenX375(105);
    }
    return ScreenX375(75);
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return ScreenX375(10);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)getData{
    NSDictionary *parameter = @{@"ordNum":self.orderNum};
    [HttpManager postHttpRequestByGet:[NSString stringWithFormat:@"%@api/logisticsNews",REQUESTHEADER] andParameter:parameter success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"code"]];
        if ([status isEqualToString:@"200"]) {
            NSData *jsonData = [successResponse[@"data"][@"info"] dataUsingEncoding:NSUTF8StringEncoding];
            NSError *err;
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData
                                                                options:NSJSONReadingMutableContainers
                                                                  error:&err];
            for (NSDictionary *dict1 in dict[@"Traces"]) {
                LogisticsMessageModel *model = [[LogisticsMessageModel alloc] initWithDictionary:dict1 error:&err];
                [self.dataArray addObject:model];
            }
            self.code = dict[@"ShipperCode"];
            [self.tableView reloadData];
        }else {
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
    }];
}

@end
